package com.devicetracker;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

import com.google.gson.Gson;

/**
 * This class will contain different methods for calling PHP  web service APIs. 
 * 
 */
public class ApiInvoker {

	private final String DOMAIN_NAME = "http://tracker.marvelousconsultancy.com/";
	
	public boolean checkIfDeviceStolen() {
		boolean isDeviceStolen = false;
		List<NameValuePair> dataList = new ArrayList<NameValuePair>();
		User user = User.getUser();
		dataList.add(getNameValuePair("email", user.getEmail()));
		String jsonResponse = callApi("checkIfDeviceStolen", dataList);
		Gson gson=new Gson();
        String response = gson.fromJson(jsonResponse, String.class);
        Log.i("emailresponse", response);
		return isDeviceStolen;
	}
	
	/**
	 * This is general API call method which will finally call the web service
	 * method.
	 * 
	 * @param methodName Name of the method to be called.
	 */
	public String callApi(final String methodName, List<NameValuePair> nameValuePair) {
		Log.d("enter", "Api called");
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(DOMAIN_NAME+methodName + ".php");
        try {
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
		} catch (UnsupportedEncodingException e) {
			Log.e("UnsupportedEncodingException", e.getMessage());
		}
        //Execute the post and get the response
        HttpResponse response = null;
		try {
			response = httpclient.execute(httppost);
		} catch (ClientProtocolException e) {
			Log.e("ClientProtocolException", e.getMessage());
		} catch (IOException e) {
			Log.e("IOException", e.getMessage());
		}
      
        HttpEntity entity = response.getEntity();
      
        InputStream stream = null;
		try {
			stream = entity.getContent();
		} catch (IllegalStateException e) {
			Log.e("IllegalStateException", e.getMessage());
		} catch (IOException e) {
			Log.e("IOException", e.getMessage());
		}
      
		String result= convertStreamToString(stream);
		Log.i("Response", result);
		return result;
        
	}
	
	private NameValuePair getNameValuePair(final String key, final String value) {
		NameValuePair nameValuePair = new BasicNameValuePair(key, value);
		return nameValuePair;
	}
	
	public static String convertStreamToString(InputStream is) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			Log.e("PHP Client", "Error : " + e.getMessage());
		} finally {
			try {
				is.close();
			} catch (IOException e1) {
				Log.e("PHP Client", "Error : " + e1.getMessage());
			}
		}
		return sb.toString();
	}
}
